package cz.cvut.fel.gfupan.gui;

import cz.cvut.fel.gfupan.gui.tab.DeclinationTab;
import cz.cvut.fel.gfupan.gui.tab.HeaderTab;
import cz.cvut.fel.gfupan.gui.tab.InclinationTab;
import cz.cvut.fel.gfupan.gui.tab.MireTab;
import cz.cvut.fel.gfupan.gui.tab.Tabbable;
import cz.cvut.fel.gfupan.gui.tab.TotalTab;
import javax.swing.JPanel;

public class MeasurePanel extends javax.swing.JPanel {

    private int id;
    //header, mire, inclination, declination, total
    public static final int NUMBER_OF_TABS = 5;
    public static final int HEADER_TAB = 0;
    public static final int MIRE_TAB = 1;
    public static final int DECLINATION_TAB = 2;
    public static final int INCLINATION_TAB = 3;
    public static final int TOTAL_TAB = 4;
    private Tabbable tabs[];

    /**
     * Creates new form MeasurePanel
     */
    public MeasurePanel(int id) {
        initComponents();
        this.id = id;
        initTab();

        setListeners();
    }

    public void moveToTab(int i) {
        tabbedPanel.setSelectedIndex(i);
        tabs[0].moveFocusByRow(0);
    }

    //tab: 1...6
    //row: 1...n
    public void moveToTabAndRow(int tab, int row) {
        if (tab >= 0 && tab <= NUMBER_OF_TABS) {
            moveToTab(tab - 1);
            tabs[tab - 1].moveFocusByRow(row - 1);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPanel = new javax.swing.JTabbedPane();

        tabbedPanel.setMinimumSize(new java.awt.Dimension(0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tabbedPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tabbedPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tabbedPanel;
    // End of variables declaration//GEN-END:variables

    private void setListeners() {
        for (Tabbable tab : tabs) {
            tab.setActionListeners();
            tab.setFocusListeners();
            tab.setKeyListeners();
            tab.setObservers();
        }
    }

    public void clearAll() {
        for (Tabbable tab : tabs) {
            tab.clearFields();
        }
    }

    private void initTab() {
        tabs = new Tabbable[NUMBER_OF_TABS];
        tabs[0] = new HeaderTab(this);
        tabs[1] = new MireTab(this);
        tabs[2] = new DeclinationTab(this);
        tabs[3] = new InclinationTab(this);
        tabs[4] = new TotalTab(this);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/cvut/fel/gfupan/res/Bundle"); // NOI18N

        tabbedPanel.addTab(bundle.getString("MeasurePanel.header.TabConstraints.tabTitle"), (JPanel) tabs[0]); // NOI18N
        tabbedPanel.addTab(bundle.getString("MeasurePanel.mire.TabConstraints.tabTitle"), (JPanel) tabs[1]); // NOI18N
        tabbedPanel.addTab(bundle.getString("MeasurePanel.declination.TabConstraints.tabTitle"), (JPanel) tabs[2]); // NOI18N
        tabbedPanel.addTab(bundle.getString("MeasurePanel.inclination.TabConstraints.tabTitle"), (JPanel) tabs[3]); // NOI18N
        tabbedPanel.addTab(bundle.getString("MeasurePanel.total.TabConstraints.tabTitle"), (JPanel) tabs[4]); // NOI18N
    }
}
